package concurrency2;

import cn.hutool.core.date.DateUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SchueduleExecutorDemo {

    private static final ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1);

    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        // 2秒后1秒执行一次
        executor.scheduleWithFixedDelay(() -> {
            if (hasMsgAtCurrentTime()) {
                System.out.println(df.format(new Date()));
                System.out.println("大家注意了，我要发消息了");
            }
        }, 2, 1, TimeUnit.SECONDS);

        // 2s后执行一次
        executor.schedule(()-> System.out.println(DateUtil.now()), 2, TimeUnit.SECONDS);
    }

    public static boolean hasMsgAtCurrentTime() {
        return true;
    }
}
